{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5690b929",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from gs_quant.session import GsSession, Environment\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.instrument import IRSwap\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from gs_quant.instrument import Instrument"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1e82503",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40ca1564",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "IRSwap?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e14778db",
   "metadata": {},
   "source": [
    "The Portfolio.from_csv mappings is a dictionary mapping a gs-quant instrument field to your csv column name (columns must not have duplicates). Mappings can also be a callable lambda function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb9febfb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "mappers = {'type': lambda row: IRSwap.type_.value, 'asset_class': lambda row: IRSwap.asset_class.value,\n",
    "           'effective_date': 'EffectiveDate',\n",
    "           'pay_or_receive': lambda row: 'Pay' if float(row['Notional'].replace(',', '')) < 0 else 'Receive',\n",
    "           'termination_date': 'EndDate',\n",
    "           'fixed_rate': 'Coupon/Spread',\n",
    "           'notional_amount': 'Notional',\n",
    "           'notional_currency': 'CCY1',\n",
    "           'roll_convention': lambda row: 'IMM' if row['Roll Conv'] == 'IMM' else None,\n",
    "           'fixed_rate_frequency': lambda row: '3m' if row['Frequency'] == 'QUARTERLY' else '6m'\n",
    "           }\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "704dabf0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "p = Portfolio.from_csv('my_excel_portfolio.csv', mappers)\n",
    "p.resolve()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0881ba6",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "p[0].as_dict()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
